﻿using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WpfKinectUserControlSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {


        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        #endregion

        #region Fields
        private KinectSensorChooser sensorChooser;
        private KinectSensor kinect;
        private WriteableBitmap colorBitmap;
        private byte[] colorPixels;
        private DrawingGroup drawingGroup;
        private DrawingImage imageSource;
        #endregion

        #region Properties
        public KinectSensorChooser SensorChooser
        {
            get { return sensorChooser; }
            set { sensorChooser = value; OnPropertyChanged(new PropertyChangedEventArgs("SensorChooser")); }
        }
        public KinectSensor Kinect
        {
            get { return kinect; }
            set { kinect = value; OnPropertyChanged(new PropertyChangedEventArgs("Kinect")); }
        }
        #endregion
        public MainWindow()
        {

            InitializeComponent();
            this.DataContext = this;

        }
        #region Events
        private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args)
        {
            MessageBox.Show(args.NewSensor == null ? "No Kinect" : args.NewSensor.Status.ToString() + " " + args.NewSensor.UniqueKinectId, "Sensor");
            if (args.OldSensor != null)
            {
                args.OldSensor.DepthStream.Disable();
                args.OldSensor.SkeletonStream.Disable();
                args.OldSensor.ColorStream.Disable();
                args.OldSensor.Stop();
            }
            if (args.NewSensor != null && args.NewSensor.Status == KinectStatus.Connected)
            {
                args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                args.NewSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                args.NewSensor.SkeletonStream.Enable();
                this.colorPixels = new byte[args.NewSensor.ColorStream.FramePixelDataLength];
                this.colorBitmap = new WriteableBitmap(args.NewSensor.ColorStream.FrameWidth, args.NewSensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
                this.ColorImage.Source = colorBitmap;
                args.NewSensor.AllFramesReady += this.SensorAllFramesReadyKinect;
                Kinect = args.NewSensor;
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.drawingGroup = new DrawingGroup();
            this.imageSource = new DrawingImage(this.drawingGroup);
            this.sensorChooser = new KinectSensorChooser();
            this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
            this.sensorChooserUi.KinectSensorChooser = this.sensorChooser;
            this.sensorChooser.Start();
        }
        private void SensorAllFramesReadyKinect(object sender, AllFramesReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorPixels);

                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        this.colorPixels,
                        this.colorBitmap.PixelWidth * sizeof(int),
                        0);
                }
            }
        }
        #endregion


    }
}
